<?php

/**
 * servicio actions.
 *
 * @package    AltargetEcommerce
 * @subpackage servicio
 * @author     Carlo Lopez
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class servicioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->servicios = Doctrine_Core::getTable('Servicio')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->servicio = Doctrine_Core::getTable('Servicio')->find(array($request->getParameter('nombre')));
    $this->forward404Unless($this->servicio);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ServicioForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ServicioForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($servicio = Doctrine_Core::getTable('Servicio')->find(array($request->getParameter('nombre'))), sprintf('Object servicio does not exist (%s).', $request->getParameter('nombre')));
    $this->form = new ServicioForm($servicio);
  }

  /**
   * Se actualiza el contenido de un servicio de acuerdo a la informacion
   * suministrada en la accion de edicion.
   * 
   * @param sfWebRequest $request 
   */
  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($servicio = Doctrine_Core::getTable('Servicio')->find(array($request->getParameter('nombre'))), sprintf('Object servicio does not exist (%s).', $request->getParameter('nombre')));
    $this->form = new ServicioForm($servicio);

    $fileName = $this->form->getValue('nombre_imagen');
    //$this->getRequest()->moveFile('nombre_imagen', sfConfig::get('sf_upload_dir').'/'.$fileName);
    
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($servicio = Doctrine_Core::getTable('Servicio')->find(array($request->getParameter('nombre'))), sprintf('Object servicio does not exist (%s).', $request->getParameter('nombre')));
    $servicio->delete();

    $this->redirect('servicio/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $file = $this->form->getValue('imagen');
      
      $filename = 'service_'.sha1($file->getOriginalName());
      $extension = $file->getExtension($file->getOriginalExtension());
      //$file->save('/'.$filename.$extension);
      
            
      
      $file_path = sfConfig::get('sf_upload_dir').'/' . $filename . $extension;
      $file->save( $file_path ,'image/jpg');
     // $file->save( $filename . $extension ,'image/jpg');
      
      $servicio = $form->save();

      $this->redirect('servicio/edit?nombre='.$servicio->getNombre());
    }
  }
}
